# TODO: Add comment
# 
# Author: broken
###############################################################################
library(raster)
library(base)
library(gstat)
library(stringr)

inputPath = "E:/FIMO/DATA/PM/ProdMODMF"
outputPath = "E:/FIMO/DATA/PM/MOD04/"

listFile = list.files(path = inputPath,pattern=".tif$",full.names = TRUE,recursive=TRUE)
listSize = length(listFile)
dir.create(file.path(outputPath))
for(i in 1:listSize){
	rasterFile=raster(listFile[i])
	rasterValues=values(rasterFile)
	valid_pixel=sum(rasterValues!=-9999)
	#valid = sum(!is.na(rasterValues))
	ratio = valid_pixel/2024*100
	year=substr(listFile[i],27,30)

	outFile=paste(outputPath,year,"/",sep="")
	#dir.create(file.path(outFile),recuse=TRUE)
	if(ratio>=30){
		print(listFile[i])
		file.copy(listFile[i],outFile)
	
	}
	
}
print("Done")
